/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import pl.milib.data.info.MIEventInfo;
import pl.milib.data.info.ScrollInfo;
import pl.milib.mc.abstract.MIScroller;
import pl.milib.mc.service.MIButton;
import pl.milib.mc.service.MIMC;
import pl.milib.mc.virtualUI.VirtualDrag;
import pl.milib.util.MIMathUtil;

/** @author Marek Brun 'minim' */
class pl.milib.mc.ScrollerSimple extends MIScroller {

	private var mc_cursor : MovieClip;
	private var cursorLength : Number;
	private var bgLength : Number;
	private var viDrag : VirtualDrag;
	private var mimc : MIMC;
	private var btn : MIButton;
	private var btnUP : MIButton;
	private var btnDN : MIButton;
	
	public function ScrollerSimple(mc:MovieClip, $btnUP:Button, $btnDN:Button) {
		mimc=MIMC.forInstance(mc);
		mc_cursor=mimc.g('cursor');
		cursorLength=mimc.g('cursor.settLen')._height;
		mimc.g('cursor.settLen').unloadMovie();
		bgLength=mimc.g('settLen')._height;
		mimc.g('settLen').unloadMovie();
		
		viDrag=new VirtualDrag(mc_cursor);
		viDrag.addListener(this);
		btn=mimc.getMIButton('cursor.btn');
		btn.addListener(this);
		if($btnUP){
			btnUP=MIButton.forInstance($btnUP);
			btnUP.addListener(this);
		}
		if($btnDN){
			btnDN=MIButton.forInstance($btnDN);
			btnDN.addListener(this);
		}
	}//<>
	
	private function setupUPDNButtons(btnUP, btnDN):Void {
	}//<<
	
	public function getIsPressed(Void):Boolean {
		return btn.getIsPressed();
	}//<<
	
	public function applyScrollData(scrollData:ScrollInfo):Void {
		mc_cursor._y=(bgLength-cursorLength)*scrollData.scrollN01;
		//mimc.mc._visible=scrollData.total!=scrollData.visible;
		btn.setIsEnabled(scrollData.total>scrollData.visible);
		mimc.mc._visible=scrollData.total>scrollData.visible;
	}//<<
	
//****************************************************************************
// EVENTS for ScrollerSimple
//****************************************************************************
	private function onEvent(ev:MIEventInfo):Void{
		super.onEvent(ev);
		switch(ev.hero){
			case viDrag:
				switch(ev.event){
					case viDrag.event_Start:
						scrollable.removeListener(this);
					break;
					case viDrag.event_NewDragXY:
						var scroll01:Number=MIMathUtil.b01(viDrag.getY()/(bgLength-cursorLength));
						scrollable.setScrollN01(scroll01);
						var sd:ScrollInfo=scrollable.getScrollData();
						sd.scrollN01=scroll01;
						applyScrollData(sd);
						bev(event_Moved);
					break;
					case viDrag.event_Stop:
						scrollable.addListener(this);
					break;
				}
			break;
			case btn:
				switch(ev.event){
					case btn.event_Press:
						viDrag.start();
					break;
					case btn.event_ReleaseAll:
						viDrag.stop();
					break;
				}
			break;
			case btnDN:
				switch(ev.event){
					case btnDN.event_Press:
						scrollable.setScrollByOneUnit(1);
					break;
				}
			break;
			case btnUP:
				switch(ev.event){
					case btnUP.event_Press:
						scrollable.setScrollByOneUnit(-1);
					break;
				}
			break;
		}
	}//<<Events
	
}